package com.example.reactor.guide.chapter3.repository;

import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zpMeng
 * @since 2024/4/30
 */
@Repository
public class EmployeeRepository {

    static Map<String, Employee> data = new LinkedHashMap<>();

    static {
        for (int i = 0; i < 100; i++) {
            data.put(String.valueOf(i), new Employee(String.valueOf(i), "name " + i));
        }
    }

    public Mono<Employee> findEmployeeById(String id) {
        return Mono.just(data.get(id));
    }

    public Flux<Employee> findAllEmployees() {
        return Flux.fromStream(data.values().stream());
    }

    public Mono<Employee> updateEmployee(Employee employee) {
        Employee oldData = data.put(employee.id(), employee);
        return Mono.justOrEmpty(oldData);
    }
}
